package com.company.base.web.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang3.StringEscapeUtils;

import com.company.base.utils.CheckUtil;
import com.company.site.mode.common.Page;

public class PageTag extends SimpleTagSupport {
	
	private String action = null;
	
	private int pageSize = -1;
	
	@SuppressWarnings("rawtypes")
	private Page page = null;

	private static final int centerShowNum = 5;

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = getJspContext().getOut();

		try {
			String html = generateHTML();
			out.print(html);
		} catch (IOException e) {
			throw new JspException(e);
		}
	}

	private String generateHTML() {

		PageContext pageContext = (PageContext) getJspContext();
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		Boolean isHtml = true;

		if (page == null || page.getTotalPages() < 1) {
			return "";
		} else if (page.getTotalPages() == 1) {
			return "";
		}

		StringBuilder htmlStr = new StringBuilder();

		// htmlStr.append("<div class=\"announ_pager\">");
		if (page.isHasPre()) {
			htmlStr.append(genLinkHtml(
					genHref(request, page.getPrePage(), isHtml), "&larr;上一页", isHtml,
					"previous", page.getPrePage()));
		} else {
			htmlStr.append(genNoHrefLinkHtml(
					genHref(request, page.getPrePage(), isHtml), "首    页", isHtml,
					"previous"));
		}
		if (page.isHasNext()) {
			htmlStr.append(genLinkHtml(
					genHref(request, page.getNextPage(), isHtml), "下一页 &rarr;",
					isHtml, "next", page.getNextPage()));
		} else {
			htmlStr.append(genNoHrefLinkHtml(
					genHref(request, page.getTotalPages(), isHtml), "末    页",
					isHtml, "next"));
		}
		return htmlStr.toString();
	}

	private String genLinkHtml(String href, String text, Boolean isHtml,
			String cls, int pageNo) {
		StringBuilder linkHtmlStr = new StringBuilder();
		linkHtmlStr.append("<li>").append("<a");
		if (isHtml && cls != null) {
			linkHtmlStr.append(" class=\"").append(cls).append("\"");
		}
		linkHtmlStr.append(" href=\"").append(href).append("\">").append(text)
				.append("</a></li>");
		return linkHtmlStr.toString();
	}

	private String genNoHrefLinkHtml(String href, String text, Boolean isHtml,
			String cls) {
		StringBuilder linkHtmlStr = new StringBuilder();
		linkHtmlStr.append("<li>").append("<a");
		if (isHtml && cls != null) {
			linkHtmlStr.append(" class=\"").append(cls).append("\"");
		}
		linkHtmlStr.append(">").append(text).append("</a></li>");
		return linkHtmlStr.toString();
	}

	private String genHref(HttpServletRequest request, long pageNo,
			Boolean isHtml) {
		StringBuilder href = new StringBuilder();
		String contextPath = request.getContextPath();
		if (action.equals("javascript:;")) {
			href.append(action);
		} else {
			if (!CheckUtil.isEmpty(contextPath)) {
				href.append(request.getContextPath()).append('/')
						.append(action);
			} else {
				href.append(request.getContextPath()).append(action);
			}
		}
		href.append(action.contains("?") ? '&' : '?').append("pageNo=")
				.append(pageNo);
		if (pageSize != -1) {
			href.append("&pageSize=").append(pageSize);
		}
		return StringEscapeUtils.escapeHtml3(href.toString());
	}

	public void setAction(String action) {
		this.action = action;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPage(@SuppressWarnings("rawtypes") Page page) {
		this.page = page;
	}

}
